{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# one hot\n",
    "\n",
    "look at \"### One Hot\" section. Same results as \"### Embeddings\"\n",
    "\n",
    "https://medium.com/@ilblackdragon/tensorflow-tutorial-part-3-c5fc0662bc08\n",
    "\n",
    "\n",
    "Also look at random.seed(x) set pre-data import. Is this the initialization seed that Kenny and I discussed? "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "import pandas\n",
    "import numpy as np\n",
    "from sklearn import metrics, cross_validation\n",
    "\n",
    "import tensorflow as tf\n",
    "from tensorflow.contrib import layers\n",
    "from tensorflow.contrib import learn\n",
    "\n",
    "random.seed(42)\n",
    "\n",
    "data = pandas.read_csv('data/titanic_train.csv')\n",
    "X = data[[\"Embarked\"]]\n",
    "y = data[\"Survived\"]\n",
    "X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, y, test_size=0.2, random_state=42)\n",
    "\n",
    "embarked_classes = X_train[\"Embarked\"].unique()\n",
    "n_classes = len(embarked_classes) + 1\n",
    "print('Embarked has next classes: ', embarked_classes)\n",
    "\n",
    "cat_processor = learn.preprocessing.CategoricalProcessor()\n",
    "X_train = np.array(list(cat_processor.fit_transform(X_train)))\n",
    "X_test = np.array(list(cat_processor.transform(X_test)))\n",
    "\n",
    "### Embeddings\n",
    "\n",
    "EMBEDDING_SIZE = 3\n",
    "\n",
    "def categorical_model(features, target):\n",
    "    target = tf.one_hot(target, 2, 1.0, 0.0)\n",
    "    features = learn.ops.categorical_variable(\n",
    "        features, n_classes, embedding_size=EMBEDDING_SIZE, name='embarked')\n",
    "    prediction, loss = learn.models.logistic_regression(tf.squeeze(features, [1]), target)\n",
    "    train_op = layers.optimize_loss(loss,\n",
    "        tf.contrib.framework.get_global_step(), optimizer='SGD', learning_rate=0.05)\n",
    "    return tf.argmax(prediction, dimension=1), loss, train_op\n",
    "\n",
    "classifier = learn.Estimator(model_fn=categorical_model)\n",
    "classifier.fit(X_train, y_train, steps=1000)\n",
    "\n",
    "print(\"Accuracy: {0}\".format(metrics.accuracy_score(classifier.predict(X_test), y_test)))\n",
    "print(\"ROC: {0}\".format(metrics.roc_auc_score(classifier.predict(X_test), y_test)))\n",
    "\n",
    "### One Hot\n",
    "\n",
    "def one_hot_categorical_model(features, target):\n",
    "    target = tf.one_hot(target, 2, 1.0, 0.0)\n",
    "    features = tf.one_hot(features, n_classes, 1.0, 0.0)\n",
    "    prediction, loss = learn.models.logistic_regression(\n",
    "      tf.squeeze(features, [1]), target)\n",
    "    train_op = layers.optimize_loss(loss,\n",
    "        tf.contrib.framework.get_global_step(), optimizer='SGD',\n",
    "        learning_rate=0.01)\n",
    "    return tf.argmax(prediction, dimension=1), loss, train_op\n",
    "\n",
    "classifier = learn.Estimator(model_fn=one_hot_categorical_model)\n",
    "classifier.fit(X_train, y_train, steps=1000)\n",
    "\n",
    "print(\"Accuracy: {0}\".format(metrics.accuracy_score(classifier.predict(X_test), y_test)))\n",
    "print(\"ROC: {0}\".format(metrics.roc_auc_score(classifier.predict(X_test), y_test)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15rc1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
